package org.dx.oho.book.two;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.dx.oho.book.two.pojo.Area;

import java.io.IOException;
import java.io.InputStream;

/**
 * create by duanx 2021/6/3
 *@description： todo
 *@classname: Mybatis
 *@author: duanx
 *@date: 2021/6/3 16:04
 **/
public class Mybatis {

    /**
     * 获取SqlSessionFactory
     * @return session工厂
     */
    public static SqlSession getSqlSession() {
        SqlSessionFactory factory = null;
        String resource = "mybatis-config.xml";
        InputStream inputStream ;
        SqlSession session = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
            factory = new SqlSessionFactoryBuilder().build(inputStream);
            session = factory.openSession();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return session;
    }

    public static void main(String[] args) {
        SqlSession session = getSqlSession();
        Area area = session.selectOne("package org.dx.oho.book.two.mapper.getAreaById",1);
        System.out.println(area.toString());
    }

}
